﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ExamManagementSystem
{
    public partial class AddKnowledgeForm : Form
    {
        public AddKnowledgeForm()
        {
            InitializeComponent();
        }
        //填充科目名
        private void AddKnowledgeForm_Load(object sender, EventArgs e)
        {
            SqlConnection conn = DBHelper.connection;
            conn.Open();
            string strSql = "select SubjectName from mySubject";
            SqlCommand cmdsubject = new SqlCommand(strSql, conn);
            SqlDataReader dr = cmdsubject.ExecuteReader();
            while (dr.Read())
            {
                this.cbmSubject.Items.Add(dr[0]);
            }
            conn.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            String Knowledge = this.txtKnowledge.Text.ToString();
            String SubjectName = this.cbmSubject.Text.ToString();
            if (Knowledge == null||SubjectName==null)
                MessageBox.Show("不能为空！");
            else
            {
                //获取SubjectId
                SqlConnection conn = DBHelper.connection;
                conn.Open();
                string strSql = "select * from mySubject where SubjectName='" + SubjectName + "'";
                SqlCommand cmdsubid = new SqlCommand(strSql, conn);
                int isubjectid = Convert.ToInt32(cmdsubid.ExecuteScalar());
                conn.Close();

                conn.Open();
                strSql="insert into Knowledge values('"+Knowledge+"',"+isubjectid+")";
                SqlCommand cmdAdd = new SqlCommand(strSql,conn);
                if ((int)cmdAdd.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("添加成功");
                    this.txtKnowledge.Text = "";
                }
                else
                {
                    MessageBox.Show("添加失败");
                    this.txtKnowledge.Text = "";
                }
                conn.Close();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
        }
    }
}
